package org.example.leetcode.core.controller;



import com.aliyun.core.annotation.Path;
import com.aliyun.sdk.gateway.pop.interceptor.response.PopResInterceptor;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.leetcode.common.domain.SysUser;
import org.example.leetcode.common.pojo.GlobalResponse;
import org.example.leetcode.common.utils.UserInfo;
import org.example.leetcode.core.domain.SysResource;
import org.example.leetcode.core.dto.MenuResource;
import org.example.leetcode.core.dto.SysUserRegisterDto;
import org.example.leetcode.core.dto.SysUserTokenDto;
import org.example.leetcode.core.dto.UserInfoVo;
import org.example.leetcode.core.service.SysUserService;
import org.example.leetcode.core.service.impl.login.MailLogin;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhc
 * @since 2024-02-18
 */
@RestController
@RequestMapping("/sys-user")
public class SysUserController{
    @Resource
    private SysUserService sysUserService;
    @Resource
    private MailLogin mailLogin;

    /**
     * 登录或注册
     * @param type
     * @param phone
     * @param password
     * @return
     */
    @GetMapping("/login/{type}/{phone}/{password}")
    public GlobalResponse<SysUserTokenDto> loginOrRegister(@PathVariable("type")String type, @PathVariable("phone")String phone, @PathVariable(value = "password")String password){
        //todo 密码加密
        return GlobalResponse.success(sysUserService.loginOrRegister(type,phone,password));
    }
    @GetMapping("/logout")
    public GlobalResponse<Boolean> logout(){
        return GlobalResponse.success(sysUserService.logout());
    }

    /**
     * 检查是否是第一次登录，在验证码登录前先调用该接口判断是否登录后要设置密码
     * @param phone
     * @return
     */
    @GetMapping("/checkFirstTimeLogin")
    public GlobalResponse<Boolean> checkFirstTime(@RequestParam("phone") String phone){
        return GlobalResponse.success(sysUserService.checkFirstTimeLogin(phone));
    }

    /**
     * 发送验证码
     * @param phone
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping("/sendMail/{type}")
    public GlobalResponse<Boolean> sendMail(@PathVariable("type")String type,@RequestParam("phone")String phone) throws ExecutionException, InterruptedException {
        mailLogin.send(type,phone);
        return GlobalResponse.success(Boolean.TRUE);
    }

    /**
     * 给token续期
     * @param phone
     * @return
     */
    @GetMapping("/refreshToken")
    public GlobalResponse<String> refreshToken(@RequestParam("phone")String phone)
    {
        return GlobalResponse.success(sysUserService.refreshToken(phone));
    }
    @PostMapping("/register")
    public GlobalResponse<Boolean> register(@RequestBody SysUserRegisterDto sysUserRegisterDto){
        sysUserService.register(sysUserRegisterDto);
        return GlobalResponse.success(Boolean.TRUE);
    }
    @PostMapping("/setPassword")
    public GlobalResponse<Boolean> setPassword(@RequestBody SysUserRegisterDto sysUserRegisterDto){
        sysUserService.setPassword(sysUserRegisterDto);
        return GlobalResponse.success(Boolean.TRUE);
    }

    @GetMapping("/getResource")
    public GlobalResponse<List<MenuResource>> getResource(@RequestParam("phone")String phone, @RequestParam("type") String type){
        return GlobalResponse.success(sysUserService.getResource(phone,type));

    }
    @GetMapping("/userInfo")
    public GlobalResponse<UserInfoVo> getUserInfo()
    {
        return GlobalResponse.success(sysUserService.getUserInfo());
    }
    @PostMapping("/saveDetail")
    public GlobalResponse<Boolean> savaDetail(@RequestBody SysUser sysUser){
        return GlobalResponse.success(sysUserService.saveDetail(sysUser));
    }
    @GetMapping("/getPic")
    public GlobalResponse<String> getPic(){
        SysUser userInfo = UserInfo.getUserInfo();
        SysUser sysUser = sysUserService.getBaseMapper().selectOne(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getPhone, userInfo.getPhone()));
        return GlobalResponse.success(sysUser.getPic());
    }

}

